<?php

namespace Home\Controller;

/**
 * Description of SynPageController.class控制器实例
 *
 * @author zhangzhen	
 * @date 2016-12-22
 */
class SynPageController extends ParentController {
	
	/**
     * @description 集群视图
     * @modify zhangzhen 2016-12-26
     */
	public function index(){
		session('master_id',null);
		$synLogic = A('Common/Syn', 'Logic');
		$hotelSynDetail = $synLogic ->getHotelSynDetail();
		$this->assign('hotelSynDetail',$hotelSynDetail);
		$this->display();
	}

    /**
     * @description 集群视图
     * @modify zhangzhen 2016-12-22
     */
	public function community(){
		$masterId = session('master_id');
		if(empty($masterId)) redirect('index.html');
		Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('MANAGE_SYN_URL'));
        $client->setEncryptMode(3);
        $sendData = array(
            'flag' => 'uclbrt',
            'time' => time(),
            'masterId' => $masterId,
        );
		
        $clientData = $client->checkCommunity($sendData);
		$synLogic = A('Common/Syn', 'Logic');
		$result = $synLogic ->synCommunityDetail($clientData);
		$this->assign('community',$result);

		// 获取已申请客栈
		$userId = getUserId();
		$adminInfo = D('AdminLogin')->getInfo($userId);
		$applyHotel = D('HotelApply')->getInfoByMobileAndAreaCode($adminInfo['mobile'], $adminInfo['areacode'], array(0,2));
		$this->assign('applyHotel',$applyHotel);
		
		//获取区号时区数据
        $HotelLogic = A('Common/Hotel', 'Logic');
        $universalTimeData = $HotelLogic->universalTimeList();
        $this->assign('universalTimeList', $universalTimeData);

        // 获取用户管理客栈id
        $AdminHotelModel = D('AdminHotel');
        $adminHotelInfo = $AdminHotelModel->getInfosByAdmin($userId);
        // 获取系统货币
        $currencyLogic = A('Common/Currency', 'Logic');
        $currencyData = $currencyLogic->getBpCurrencyAndDefault($adminHotelInfo[0]['hotelentity_id']);
        $currencyDefaultData = my_sort($currencyData, 'type', SORT_DESC)[0];
        $currencyDefaultData = $currencyDefaultData['type'] == 1 ? $currencyDefaultData : [];

        $this->assign('currencyInfo', $currencyData);
        $this->assign('currencyDefaultInfo', $currencyDefaultData);

		$this->display();
	}
	/**
     * @description 房间与子管视图
     * @modify zhangzhen 2016-12-22
     */
	public function import(){
		$masterId = session('master_id');
		$hotelId = I('get.hotel');
		$hotelSynId = I('get.hotelSynId');
		if(empty($masterId)||empty($hotelId)||empty($hotelSynId)) redirect('index.html');
		$userId = getUserId();
		//获取客栈名称
		$hotelEntityModel = D('HotelEntity');
		$hotelEntityDetail = $hotelEntityModel->getInfo($hotelId);
		
		//获取客栈同步信息
		$hotelSynModel = D('HotelSyn');
		$hotelSynDetail = $hotelSynModel->getInfoById($hotelSynId);
		
		//获取房间同步信息
		$synLogic = A('Common/Syn', 'Logic');
		$roomDetail = $synLogic ->getSynRoom($hotelId);
		
		//RPC 获取集群管理员
		Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('MANAGE_SYN_URL'));
        $client->setEncryptMode(3);
        $sendData = array(
            'flag' => 'uclbrt',
            'time' => time(),
            'communityId' => $hotelSynDetail['masterentity_id'],
        );
		$data = $client->getUserCommunityAndRoom($sendData);
		//房间数据
		$rooms = $data['room'];
		
		$roomFormat = $synLogic ->getImportRoom($hotelId, $rooms);
		
		//集群名称
		$communityName = $data['communityName'];
		//复制一份子管理数据
		$copyClientData = $clientData = $data['users'];
		
		//过滤美住本来已有的管理员
		$accountLogic = A('Common/Account', 'Logic');
        $accounts = $accountLogic->getSub($userId);
		foreach($clientData as $key => $oneData){
			$clientData[$key]['masterentity_id'] = $hotelSynDetail['masterentity_id'];
			foreach($accounts as $account){
				if($oneData['mobile'] == $account['mobile']){
					unset($clientData[$key]);
				}
			}
		}
		
		//获取管理员同步信息
		$adminSynModel = D('AdminSyn');
		$adminSynDetail = $adminSynModel->getInfoByHotelId($hotelId);
		//筛选出锁掌柜删除的管理员
		foreach($adminSynDetail as $key => $oneAdminSyn){
			foreach($copyClientData as $oneData){
				if($oneAdminSyn['usercommunity_id'] == $oneData['usercommunity_id']){
					$adminSynDetail[$key]['notdelete'] = 1;
					break;
				}
			}
		}
		//集群名和客栈名
		$synName = array(
			'communityName' => $communityName,
			'hotelName' => $hotelEntityDetail['name']
		);

		// 是否是义卖客栈
        $result = $this->checkUsageHotel($hotelId);
        // 是否开启畅联代收
        $isPayUclbrt = $this->checkIsUclbrtPay($hotelId);

        $this->assign('isPayUclbrt', $isPayUclbrt);
        $this->assign('isFreeHotel', $result['isFreeHotel']);

		$this->assign('synName',$synName);
		$this->assign('roomFormat',$roomFormat);
		$this->assign('account',$clientData);
		$this->assign('roomDetail',$roomDetail);
		$this->assign('adminSyn',$adminSynDetail);
		$this->display();
	}
   

}
